package com.example.touristassist;

import android.os.Bundle;
import android.preference.PreferenceManager;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.View;
import android.widget.EditText;


public class SettingActivity extends Activity{

	private EditText et_host;
	private EditText et_port;
	private EditText et_service;
	SharedPreferences prefs;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.setting_layout);		
		et_host = (EditText)findViewById(R.id.et_setting_host);
		et_port = (EditText)findViewById(R.id.et_setting_port);
		et_service = (EditText)findViewById(R.id.et_setting_service);	
		prefs = PreferenceManager.getDefaultSharedPreferences(this);	
		
		bindValueToView();
	}
	
	public void onClick(View target)
	{
		Intent result = new Intent("SettingActivity");
		if(target.getId() == R.id.btn_setting_ok)
		{
			String resultText = "";
			try{
				SharedPreferences.Editor editor = prefs.edit();
				editor.putString(AppConfig.HOST_PREF, et_host.getText().toString());
				editor.putInt(AppConfig.PORT_PREF, Integer.parseInt(et_port.getText().toString()));
				editor.putString(AppConfig.SERVICE_PREF, et_service.getText().toString());
				editor.commit();	
				resultText = this.getString(R.string.setting_save_success);
			}
			catch(Exception ex)
			{
				Log.e("Setting activity", ex.getMessage() + ex.getStackTrace());
				resultText = this.getString(R.string.setting_save_failure);
			}
						
			result.putExtra(AppConfig.SETTING_RESULT, resultText);
			setResult(Activity.RESULT_OK, result);			
		}
		else
		{
			setResult(Activity.RESULT_CANCELED, result);
		}
		finish();
	}
	
	private void bindValueToView(){
		
		String host = prefs.getString(AppConfig.HOST_PREF, "0");
		int port = prefs.getInt(AppConfig.PORT_PREF, 0);
		String service = prefs.getString(AppConfig.SERVICE_PREF, "0");
		if(!host.equals("0"))
			et_host.setText(host);
		if(port != 0)
			et_port.setText(String.valueOf(port));
		if(!service.equals("0"))
			et_service.setText(service);
	}
	
	// AsyncTask try to connect XMPP
   /* class TryToConnectXMPPServer extends AsyncTask<Void, Void, Void> {
	    	
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
		}

		@Override
		protected Void doInBackground(Void... arg0) {
			
			String host = et_host.getText().toString();
		    String port = et_port.getText().toString();
		    String service = et_service.getText().toString();
			// Create a connection
	        ConnectionConfiguration connConfig =
	                new ConnectionConfiguration(host, Integer.parseInt(port), service);
	        XMPPConnection connection = new XMPPConnection(connConfig);

	        try {
	            connection.connect();
	            Log.i("XMPPClient", "[SettingsDialog] Connected to " + connection.getHost());
	        } catch (XMPPException ex) {
	            Log.e("XMPPClient", "[SettingsDialog] Failed to connect to " + connection.getHost());
	            Log.e("XMPPClient", ex.toString());
	        }
			
			return null;
		}
		
		@Override
		protected void onProgressUpdate(Void... values) {
			super.onProgressUpdate(values);
		}
		
		@Override
		protected void onPostExecute(Void result) {
			super.onPostExecute(result);
			
		}

	}*/
	
}
